package com.dongdongshop.controller;

import com.dongdongshop.domain.AjaxResult;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.UserService;
import com.dongdongshop.vo.AddressVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("addressController")
public class AddressController {

    @DubboReference
    private UserService userService;

    //获取全部地址信息
    @RequestMapping("address")
    @ResponseBody
    public AjaxResult<List<AddressVo>> address() {
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        String username = user.getUsername();
        try {
            List<AddressVo> addressList = userService.getAddress(username);
            return AjaxResult.OK().setData(addressList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //获取省
    @RequestMapping("getProvince")
    @ResponseBody
    public AjaxResult getProvince(String id) {
        try {
            List<TbProvinces> list = userService.getProvince(id);
            return AjaxResult.OK().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //获取市
    @RequestMapping("getCity")
    @ResponseBody
    public AjaxResult getCity(String id) {
        try {
            List<TbCities> list = userService.getCity(id);
            return AjaxResult.OK().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //获取区
    @RequestMapping("getDistrict")
    @ResponseBody
    public AjaxResult getDistrict(String id) {
        try {
            List<TbAreas> list = userService.getDistrict(id);
            return AjaxResult.OK().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //增加地址信息
    @RequestMapping("addAddress")
    @ResponseBody
    public AjaxResult addAddress(TbAddress address) {
        if (address == null) {
            return AjaxResult.R(ResultEnum.PARAMETER_EMPTY);
        }
        try {
            userService.addAddress(address);
            return AjaxResult.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }


    //删除地址信息
    @RequestMapping("deleteAddress")
    @ResponseBody
    public AjaxResult deleteAddress(Long id) {
        if (id == null) {
            return AjaxResult.R(ResultEnum.PARAMETER_EMPTY);
        }
        try {
            userService.deleteAddress(id);
            return AjaxResult.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //回显地址信息
    @RequestMapping("editAddress")
    @ResponseBody
    public AjaxResult<TbAddress> editAddress(Long id) {
        if (id == null) {
            return AjaxResult.R(ResultEnum.PARAMETER_EMPTY);
        }
        try {
            TbAddress address = userService.editAddress(id);
            return AjaxResult.OK().setData(address);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }

    //修改地址信息
    @RequestMapping("updateAddress")
    @ResponseBody
    public AjaxResult updateAddress(TbAddress address) {
        System.out.println(address);
        if (address == null) {
            return AjaxResult.R(ResultEnum.ARGS_FAILE);
        }
        try {
            userService.updateAddress(address);
            return AjaxResult.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.ERROR();
        }
    }
}
